package com.zhou.office.automation.api;

import com.zhou.office.automation.vo.ResultVO;
import com.zhou.office.automation.vo.approval.TaskVO;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 请假流程接口
 *
 * @author Zhouyongli
 * @version 2024/09/14 15:38
 **/
@Slf4j
@RestController
@RequestMapping("/leave")
public class LeaveController {

    private final RuntimeService runtimeService;
    private final TaskService taskService;
    private final RepositoryService repositoryService;
    private final ProcessEngine processEngine;

    @Autowired
    public LeaveController(RuntimeService runtimeService, TaskService taskService,
                           RepositoryService repositoryService, ProcessEngine processEngine){
        this.runtimeService = runtimeService;
        this.taskService = taskService;
        this.repositoryService = repositoryService;
        this.processEngine = processEngine;
    }

    @PostMapping("add/{day}/{studentUser")
    public ResultVO<Object> sub(@PathVariable("day") Integer day, @PathVariable("studentUser") String studentUser){
        HashMap<String, Object> map = new HashMap<>();
        map.put("day", day);
        map.put("studentUser", studentUser);

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("stuLeave", map);
        String processInstanceId = processInstance.getId();
        log.info("流程实例ID: " + processInstanceId);

        Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
        taskService.complete(task.getId());

        return new ResultVO<>(true, "提交流程成功，流程Id为：" + processInstanceId);


    }

    @GetMapping("teacherList")
    public ResultVO<List<TaskVO>> teacherList(){
        // 画流程图时辅导员审批节点"分配用户-候选组"中填写的值
        List<Task> aTasks = taskService.createTaskQuery().taskCandidateGroup("a").list();

        log.info("任务列表：" + aTasks);
        
        List<TaskVO> taskVOS = aTasks.stream().map(task -> {
            Map<String, Object> variables = taskService.getVariables(task.getId());
            return new TaskVO(task.getId(), variables.get("day").toString(), variables.get("studentUser").toString());
        }).collect(Collectors.toList());
        
        if (aTasks.isEmpty()){
            return new ResultVO<>(false,"没有任务");
        }

        return new ResultVO<>(true, "获取任务成功", taskVOS);
        
    }

    @GetMapping("teacherApply/{taskId}")
    public ResultVO<Object> teacherApply(@PathVariable("taskId") String taskId){

        Task task = taskService.createTaskQuery().taskCandidateGroup("a").taskId(taskId).singleResult();
        if (Objects.isNull(task)){
            return new ResultVO<>(false, "没有任务");
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("outcome","通过");
        taskService.complete(task.getId(), map);
        return new ResultVO<>(true, "审批成功");
    }

    @GetMapping("teacherReject/{taskId}")
    public ResultVO<Object> teacherReject(@PathVariable("taskId") String taskId){

        Task task = taskService.createTaskQuery().taskCandidateGroup("a").taskId(taskId).singleResult();
        if (Objects.isNull(task)){
            return new ResultVO<>(false, "没有任务");
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("outcome","驳回");
        taskService.complete(task.getId(), map);
        return new ResultVO<>(true, "审批失败");
    }

    @GetMapping("deanList")
    public ResultVO<List<TaskVO>> deanList(){
        // 画流程图时辅导员审批节点"分配用户-候选组"中填写的值
        List<Task> aTasks = taskService.createTaskQuery().taskCandidateGroup("b").list();

        List<TaskVO> taskVOS = aTasks.stream().map(task -> {
            Map<String, Object> variables = taskService.getVariables(task.getId());
            return new TaskVO(task.getId(), variables.get("day").toString(), variables.get("studentUser").toString());
        }).collect(Collectors.toList());

        if (aTasks.isEmpty()){
            return new ResultVO<>(false,"没有任务");
        }

        return new ResultVO<>(true, "获取任务成功", taskVOS);

    }



}
